<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var obj = {
            str: '123',
            num: 123,
            a: null,
            b: undefined,
            obj: { e: 4, d: {} },
            fn: function () {
                console.log('fn');
            },
            reg: /hello/,
            date: new Date(),
            arr: [1, '1', { a: 1, b: 3 }],
        };

        function deepClone(obj) {
            let clone = {};
            function getRegExp(re) {
                var flags = '';
                if (re.global) flags += 'g';
                if (re.ignoreCase) flags += 'i';
                if (re.multiline) flags += 'm';
                return flags;
            };
            for (const key in obj) {
                let item = obj[key];
                if (item) {
                    // console.log(Object.prototype.toString.call(item), item);
                    let typeString = Object.prototype.toString.call(item)
                    // console.log(typeString);
                    //实现方法的克隆
                    if (typeString == '[object Function]') {
                        clone[key] = new Function('return ' + item.toString())()
                    }
                    else if (typeString == '[object Object]') {
                        clone[key] = deepClone(item);
                    }
                    else if (typeString == '[object RegExp]') {
                        console.log('这是正则');
                        clone[key] = new RegExp(item,
                            getRegExp(item));
                    }
                    else {
                        clone[key] = item;
                    }
                }
            }
            return clone;
        }
        let clone = deepClone(obj);
        console.log(clone);
    </script>
</body>

</html>